//
//  OCSSOrderDetailPage.swift
//  OCSSStoreSwift
//
//  Created by MacMiniKangChe on 2018/10/16.
//  Copyright © 2018年 MacMiniKangChe. All rights reserved.
//

import UIKit
import SwiftyJSON

enum orderType {
    case outOrder
    case putInOrder
}
class OCSSOrderDetailPage: OCSSBaseViewController,UITableViewDataSource, UITableViewDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    
    var tbFooterView:OCSSBaseView?;
    var type:orderType?;
    var orderId:String?;
    var orderInfo:[String:JSON]?;
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.regTableViewNib(tableView: tableView, className: OCSSOrderDetailCell.className());
        switch type {
            case .outOrder?:
                title = "销售出库";
                tbFooterView = Bundle.main.loadNibNamed("OCSSOrderDetailFirstFooterView", owner: self, options: nil)?.last as! OCSSOrderDetailFirstFooterView;
            break
            
            case .putInOrder?:
                title = "铺货入库";
                tbFooterView = Bundle.main.loadNibNamed("OCSSOrderDetailPutInFooterView", owner: self, options: nil)?.last as? OCSSOrderDetailPutInFooterView;
            break
            
            default:
            break
        }
        
        tableView.tableFooterView = tbFooterView;
        requestForOrderDetail();
    }
    //MARK:请求订单详情数据
    func requestForOrderDetail() -> Void {
        let params = ["id":orderId];
        OCSSNetRequest.requestData(url: "/StoreHouse/ShipmentOrderDetail_app", para: params as [String : Any], type: MethodType.post, success: { (result) in
            self.orderInfo = JSON(result).dictionaryValue;
            if JSON(self.orderInfo!["Status"]!).boolValue {
                self.tableView.reloadData();
                let data = JSON(self.orderInfo!["Data"]!).dictionaryValue;
                self.tbFooterView?.setView(info: data);
                self.requestForSellerInfo();
            }
            print(result);
        }) { (error) in
            
        }
    }
    //请求获取销售员信息
    func requestForSellerInfo() -> Void {
//        let dict = JSON(UserDefaults.standard.object(forKey: "Data")).dictionaryValue;
//        
//        let userId = JSON(dict["UserIdentity"]!["UserId"]).stringValue;
//        let params = ["userId":userId];
//        OCSSNetRequest.requestData(url: "/StoreHouse/QuerySalesperson_app", para: params, type: MethodType.post, success: { (result) in
//            let dict = JSON(result).dictionaryValue;
//            if JSON(dict["Status"]!).boolValue {
//                print(JSON(dict["Data"]!["RealName"]).stringValue);
//            }
//            
//        }) { (error) in
//            
//        }
        
    }
    //MARK:UITableViewDelegate && UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if orderInfo != nil {
            let dataArr = JSON(orderInfo!["Data"]!["stockOutModel"]["Items"]).arrayValue;
            return dataArr.count;
        }
        return 0;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: OCSSOrderDetailCell.classForCoder())) as! OCSSOrderDetailCell;
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        if orderInfo != nil {
            let dataArr = JSON(orderInfo!["Data"]!["stockOutModel"]["Items"]).arrayValue;
            let dict = JSON(dataArr[indexPath.section]).dictionaryObject;
            cell.cellInfo = dict;
        }
        
        return cell;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 144;
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
